package com.workManager.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.workManager.mapper.TdMenuMapper;
import com.workManager.pojo.TdMenu;
import com.workManager.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Thinkpad on 2017/2/16 0016.
 */
@Service
public class MenuServiceImpl implements  MenuService{

    @Autowired
    private TdMenuMapper menuMapper;


    public int add(TdMenu menu) {
        return menuMapper.insert(menu);
    }

    public int delete(String menuId) {
        return menuMapper.deleteByPrimaryKey(Integer.parseInt(menuId));
//        递归删除
//        int i = 0;
//        Integer intMenuId = Integer.parseInt(menuId);
//        TdMenu menu=menuMapper.selectByPrimaryKey(intMenuId);
//
//        if (null!=menu){
//            //删除本菜单
//            menuMapper.deleteByPrimaryKey(intMenuId);
//            i++;
//            //删除子菜单
//            TdMenuExample example=new TdMenuExample();
//            example.createCriteria().andParentMenuIdEqualTo(intMenuId);
//            List<TdMenu> childMenuList=menuMapper.selectByExample(example);
//
//            if (childMenuList!=null){
//                for (TdMenu childMenu:childMenuList){
//                    if (childMenu!=null&&childMenu.getMenuId()!=null){
//                        //递归调用
//                        i+= delete(String.valueOf(childMenu.getMenuId()));
//                    }
//                }
//            }
//        }
//        return i;
    }

    public int deleteByList(List<String> menuIds) {
        int i = 0;
        for (String menuId:menuIds) {
            i+=delete(menuId);
        }
        return i;
    }

    public int update(TdMenu menu) {
        return menuMapper.updateByPrimaryKey(menu);
    }

    public PageInfo<MenuVo> query(Map<String, Object> map) {
        int pageno = map.get("pagenum")==null?1:Integer.parseInt(map.get("pagenum").toString());
        int pagesize = map.get("pagesize")==null?10:Integer.parseInt(map.get("pagesize").toString());
        PageHelper.startPage(pageno,pagesize);
        List<MenuVo> menuList = menuMapper.query(map);
        PageInfo<MenuVo> menuPageInfo = new PageInfo<MenuVo>(menuList);
        return menuPageInfo;
    }

    public PageInfo<MenuVo> queryLikeMeunName(Map<String, Object> map) {
        int pageno = !map.containsKey("pagenum")||map.get("pagenum")==null?1:Integer.parseInt(map.get("pagenum").toString());
        int pagesize = !map.containsKey("pagesize")||map.get("pagesize")==null?10:Integer.parseInt(map.get("pagesize").toString());
        PageHelper.startPage(pageno,pagesize);
        List<MenuVo> menuList = menuMapper.queryLikeMeunName(map);
        PageInfo<MenuVo> menuPageInfo = new PageInfo<MenuVo>(menuList);
        return menuPageInfo;
    }

    public List<MenuVo> queryFunctionMenu(String loginCode) {
        return menuMapper.queryFunctionMenu(loginCode);
    }

    public List<MenuVo> queryParentMenu() {
        return menuMapper.queryParentMenu();
    }
}
